package com.javaclimb.drug.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.javaclimb.drug.common.LogUtil;
import com.javaclimb.drug.common.ResultMapUtil;
import com.javaclimb.drug.entity.Druginfo;
import com.javaclimb.drug.entity.Owinfo;
import com.javaclimb.drug.entity.User;
import com.javaclimb.drug.service.IDruginfoService;
import com.javaclimb.drug.service.IOwinfoService;
import com.javaclimb.drug.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * 药品出入库相关的controller
 */
@Controller
@RequestMapping(value = "/owinfo")
public class OwinfoController {

    @Autowired
    private IOwinfoService owinfoService;
    @Autowired
    private LogService logService;
    @Autowired
    private IDruginfoService druginfoService;

    /**
     * 转向药品出入库页面
     */
    @RequestMapping
    public String owinfo(){
        return "/owinfo";
    }

    /**
     * 分页查询药品出入库信息，（关键字查询）
     */
    @RequestMapping(value = "/owinfoQueryPage")
    @ResponseBody
    public Object owinfoQueryPage(String param, String param1, @RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "10")int limit, HttpSession session){
        try{
            //根据传递来的分页信息-默认分页信息为从第一页开始，每页十个数据

            IPage<Owinfo> iPage = owinfoService.selectOwinfoPage(page,limit,param,param1);
            User user= (User) session.getAttribute("user");
            String message=user.getUsername()+":查询出入库信息";
            logService.LogInfo(LogUtil.getLogInfo(message));
            return ResultMapUtil.getHashMapMysqlPage(iPage);
        } catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向药品出入库新增页面
     */
    @RequestMapping(value = "/owinfoPage")
    public String owinfoPage(){
        return "/owinfoPage";
    }

    /**
     * 添加一个药品出入库
     */
    @RequestMapping(value = "/owinfoAdd")
    @ResponseBody
    public Object owinfoAdd(Owinfo owinfo,HttpSession session){
        try{
            User user= (User) session.getAttribute("user");
            //设置创建时间
            owinfo.setCreatetime(new Date());
            owinfo.setOperator(user.getUsername());
            //根据药品名获取药品信息
            Druginfo druginfo=druginfoService.queryDruginfoByName(owinfo.getDname(),null);
            if (druginfo!=null){
                //如果药品入库，则将该药品的库存增加；否则将库存减少
                if ("入库".equals(owinfo.getType())){
                    druginfo.setStock(druginfo.getStock()+owinfo.getCount());
                }else{
                    if(druginfo.getStock()-owinfo.getCount()>=0)
                        druginfo.setStock(druginfo.getStock()-owinfo.getCount());
                    else
                        throw new Exception();
                }
                druginfoService.updateById(druginfo);
            }

            String message=user.getUsername()+":添加药品:"+owinfo.getDname()+"  "+owinfo.getType();
            logService.LogInfo(LogUtil.getLogInfo(message));
            int i = owinfoService.addOwinfo(owinfo);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向药品出入库编辑页面
     * 传递来药品出入库信息的ID值
     */
    @RequestMapping(value = "/owinfoQueryById")
    public String owinfoQueryById(@RequestParam(name = "id",required = true)Integer id, Model model){
        //根据ID值查询药品出入库信息
        Owinfo owinfo = owinfoService.queryOwinfoById(id);
        //把查询结果添加到共享的Model中
        model.addAttribute("obj",owinfo);
        return "/owinfoPage";
    }

    /**
     * 修改一个药品出入库
     * 将前端输入的信息封装成一个实体类
     */
    @RequestMapping(value = "/owinfoEdit")
    @ResponseBody
    public Object owinfoEdit(Owinfo owinfo,HttpSession session){
        try{
            //调用业务逻辑层更新方法
            int i = owinfoService.editOwinfo(owinfo);
            User user= (User) session.getAttribute("user");
            String message=user.getUsername()+":更新"+owinfo.getDname()+"药品信息";
            logService.LogInfo(LogUtil.getLogInfo(message));
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 删除一个药品出入库信息
     * 通过前端传递来的ID值
     */
    @RequestMapping(value = "/owinfoDelById")
    @ResponseBody
    public Object owinfoDelById(Integer id,HttpSession session){
        try{
            Owinfo owinfo=owinfoService.queryOwinfoById(id);
            //调用业务逻辑层的删除方法
            int i = owinfoService.delOwinfoById(id);//返回值是删除的行数

            User user= (User) session.getAttribute("user");
            String message=user.getUsername()+":删除"+owinfo.getDname()+"  "+owinfo.getType()+"信息";
            logService.LogInfo(LogUtil.getLogInfo(message));
            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

}





















